<template>
  <div class="app-container">
      <div>
          <p class="dialogTitle title">当前地震事件:
              <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
              <span v-else>请在地震事件管理中设置当前地震事件</span>
          </p>
      </div>

      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px" v-show="showSearch">
          <el-form-item label="灾情编号" prop="disasterCode">
              <el-input v-model="queryParams.disasterCode" placeholder="请输入灾情编号" clearable />
          </el-form-item>
          <el-form-item label="灾情点名称" prop="siteName">
              <el-input v-model="queryParams.siteName" placeholder="请输入灾情点名称" clearable />
          </el-form-item>
          <el-form-item label="调查员" prop="investigator">
              <el-input v-model="queryParams.investigator" placeholder="请输入调查员" clearable />
          </el-form-item>
          <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
          </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
              <el-button type="primary" :disabled="!currentEvent||dataList.length==0" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="dataList" stripe>
          <el-table-column label="序号" align="center" width="55">
              <template #default="scope">
                  <span>{{ (queryParams.page - 1) * queryParams.rows + scope.$index + 1 }}</span>
              </template>
          </el-table-column>
          <el-table-column label="灾情编号" align="center" prop="disasterCode">
              <template #default="scope">
                  <span style="color: #107ce1;cursor: pointer;" @click="handleEdit(scope.row)">{{
                          scope.row.disasterCode
                  }}</span>
              </template>
          </el-table-column>
          <el-table-column label="灾情点名称" align="center" prop="siteName" />
          <el-table-column label="灾情点类型" align="center" prop="type">
            <template #default="scope">
                {{ getTypeName(scope.row.type) }}
              </template>
          </el-table-column>
          <el-table-column label="经度" align="center" prop="longitude" />
          <el-table-column label="纬度" align="center" prop="latitude" />
          <el-table-column label="调查员" align="center" prop="investigator" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
          @pagination="getList" />


      <el-dialog title="查看灾情急报数据" v-model="open" width="900px" append-to-body>
          <el-form ref="addRef" label-width="120px" :model="form">
              <el-row style="margin: 20px 0;">
                  <el-col :span="24">
                      <el-form-item label="行政区划" required>
                          <span>{{ form.province }}{{ form.city }}{{ form.area }}{{ form.address }}</span>
                      </el-form-item>
                  </el-col>
                  <el-col :span="12">
                      <el-form-item label="灾情编号" prop="disasterCode" required>
                          <el-input v-model="form.disasterCode" placeholder="请输入灾情编号" disabled />
                      </el-form-item>
                  </el-col>
                  <el-col :span="12">
                      <el-form-item label="灾情点名称" prop="siteName" required>
                          <el-input v-model="form.siteName" placeholder="请输入灾情点名称" disabled />
                      </el-form-item>
                  </el-col>
                  <el-col :span="12">
                      <el-form-item label="灾情点类型" prop="type" required>
                          <el-input v-model="form.type" placeholder="请输入灾情点名称" disabled />
                      </el-form-item>
                  </el-col>
                  <el-col :span="12">
                      <el-form-item label="调查员" prop="investigator" required>
                          <el-input v-model="form.investigator" placeholder="请输入调查员" disabled />
                      </el-form-item>
                  </el-col>
                  <el-col :span="12">
                      <el-form-item label="经度" prop="longitude" required>
                          <el-input v-model="form.longitude" placeholder="请输入经度" disabled />
                      </el-form-item>
                  </el-col>
                  <el-col :span="12">
                      <el-form-item label="纬度" prop="latitude" required>
                          <el-input v-model="form.latitude" placeholder="请输入纬度" disabled />
                      </el-form-item>
                  </el-col>
              </el-row>
          </el-form>
          <template #footer>
              <div class="dialog-footer">
                  <el-button @click="open = false">关 闭</el-button>
              </div>
          </template>
      </el-dialog>
  </div>
</template>

<script setup name="disasterEmergencyReportManage">
import useEventStore from "@/store/modules/event";
import { getAllList ,getDetail } from "@/api/disasterManage/fieldSurveyManage/disasterEmergencyReportManage.js"
import { getDict } from "@/api/system/dict"
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const total = ref(0);
const showSearch = ref(true);
const currentEvent = computed(() => useEventStore().currentEvent);
const data = reactive({
  queryParams: {
      page: 1,
      rows: 10,
      disasterCode: undefined,
      siteName: undefined,
      investigatorName: undefined,
  },
  form:{}
})
const { queryParams, form } = toRefs(data)

const structureTypeArr = ref([])
async function getDictData() {
	await getDict("zqlx").then(response => {
		structureTypeArr.value = response.data;
	});
}
function getTypeName(val) {
    let name = ''
    let list  = val.split(',')
    list.forEach(item => {
        let code  = structureTypeArr.value.find(it => item == it.dictItemCode)
        if(name == '') {
            name += code.dictItemName
        }else{
            name += ',' + code.dictItemName
        }
    });
    return name
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() { 
  dataList.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.downloadTemplate('/emergencyReport/exportData', { xsemId: currentEvent.value.id ,...queryParams.value}, `灾情急报数据.xls`)
}

// 编辑
function handleEdit(row){
detail(row.xderId);
}

// 查看
function detail(id){
getDetail(id).then(res=>{
  open.value = true;
  proxy.resetForm("addRef");
  form.value = res.data;
})
}

/** 查询数据列表 */
function getList() {
  if (currentEvent.value) {
      loading.value = true;
      getAllList({ ...queryParams.value, xsemId: currentEvent.value.id }).then(response => {
          dataList.value = response.data.rows ? response.data.rows : [];
          total.value = response.data.total;
          loading.value = false;
      });
  } else {
      dataList.value = [];
      total.value = 0;
  }
}

onMounted(() => {
  setTimeout(() => {
      getList();
      getDictData()
  }, 200)
})
</script>

<style scoped lang="scss">
.title {
  margin: 0 0 20px !important;
}
</style>

